package cn.melon.mod.store;

import org.apache.log4j.Logger;
import org.json.JSONException;
import org.json.JSONObject;

import cn.melon.Helper.LoggerHelper;
import cn.melon.base.BaseController;
import cn.melon.mod.store.dao.GoodsDao;
import cn.melon.mod.store.dao.impl.GoodsDaoImpl;
import cn.melon.mod.store.entity.Goods;

/**
 * store模块请求的处理
 * @author he
 */
public class StoreController extends BaseController {

    public String TAG = LoggerHelper.makeLogTag(StoreController.class);
    public Logger storeLogger = LoggerHelper.serverLogger();

    private GoodsDao goodsDao = null;
    
    private JSONObject jsonRequest = null;
    
    private JSONObject jsonResponse = new JSONObject();
    public StoreController(String request) {
        super(request);
        goodsDao = new GoodsDaoImpl();
    }

    @Override
    public void onResponse() {


        try {
            jsonRequest = new JSONObject(getRequest());
            String action = (String) jsonRequest.get("action");

            storeLogger.info(TAG + action); // log
            storeLogger.info(jsonRequest);
            switch (action) {
            case "GETGOODS":
                askGoods();
                break;
            case "ASKGOODSTABLE":
                askGoodsTable();
                break;
            case "ADDGOODS":
                askAddGoods();
                break;
            case "REMOVEGOODS":
                askRemoveGoods();
                break;
            case "UPDATEGOODS":
                askUpdateGoods();
                break;
            default:
                break;
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        setResponse(jsonResponse.toString());
    }

    /**
     *  处理请求Goods的方法 
     * void
     */
    private void askGoods(){
        String goodsId;
        try {
            goodsId = jsonRequest.getString("GoodsId");
            jsonResponse.put("Goods", goodsDao.getGoodsById(goodsId));
            jsonResponse.put("code", 200);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    
    /**
     * 处理请求商品列表的方法
     * @throws JSONException void
     */
    private void askGoodsTable() throws JSONException{
        String type = jsonRequest.getString("GoodsType");
        int start = jsonRequest.getInt("start");
        int num = jsonRequest.getInt("num");
        String goodsId = jsonRequest.getString("GoodsId");
        String goodsName = jsonRequest.getString("GoodsName");
        storeLogger.info(type);
        jsonResponse.put("GoodsTable", goodsDao.getGoodsTable(start, num, "goodsId",type,goodsId,goodsName).toJsonArray());
        jsonResponse.put("code", 200);
        storeLogger.info("返回" + jsonResponse);
    }
    
    /**
     * 处理请求添加商品的方法
     * @throws JSONException void
     */
    private void askAddGoods() throws JSONException{
        Goods goods = new Goods();
        goods.setGoodsByJsonObj(jsonRequest.getJSONObject("Goods"));
        if (goodsDao.addGoods(goods)) {
            jsonResponse.put("code", 200);
        }
    }
    
    /**
     * 处理请求删除商品的方法
     * @throws JSONException void
     */
    private void askRemoveGoods() throws JSONException{
        String goodsId = jsonRequest.getString("GoodsId");
        if (goodsDao.removeGoodsById(goodsId)) {
            jsonResponse.put("code", 200);
        }
    }
    
    /**
     * 处理请求更新商品的方法
     * @throws JSONException void
     */
    private void askUpdateGoods() throws JSONException{
        Goods goods = new Goods();
        goods.setGoodsByJsonObj(jsonRequest.getJSONObject("Goods"));
        if (goodsDao.updateGoodsInfo(goods)) {
            jsonResponse.put("code", 200);
        }
    }
}
